package com.zmn.plat.admin.controller.channel.settle;


import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.ChannelIdAndName;
import com.zmn.plat.model.entity.channel.settle.ChannelMergeSettle;
import com.zmn.plat.model.entity.channel.settle.ChannelMergeSettleQuery;
import com.zmn.plat.model.vo.channel.settle.ChannelMergeSettleVO;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import com.zmn.plat.services.interfaces.channel.settle.ChannelMergeSettleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 渠道合并结算表基础查询Controller
 *
 * @author xujie
 * @since 2021-04-13 09:27:32
 */

@Slf4j
@RestController
@RequestMapping("/channel/mergeSettle/")
public class ChannelMergeSettleListController {

    @Resource
    private ChannelMergeSettleService channelMergeSettleService;

    @Resource
    private ChannelService channelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("/channel/mergeSettle/list.action")
    public ModelAndView list(@ModelAttribute ChannelMergeSettleQuery query) {

        Integer channelId = query.getChannelId();
        List<Integer> channelIdSearchList = new ArrayList<>();
        String searchValue = query.getSearchValue();
        boolean search = true;
        if (StringUtil.isNotBlank(searchValue)) {
            String regEx = "^-?[0-9]+$";
            Pattern pat = Pattern.compile(regEx);
            Matcher mat = pat.matcher(searchValue);
            if (mat.find()) {
                int channelSearchId = Integer.parseInt(searchValue);
                if (channelSearchId > 0) {
                    channelIdSearchList.add(channelSearchId);
                }
            } else {
                channelIdSearchList = channelService.findChannelIdBySearchValue(searchValue);
            }
            if (channelIdSearchList.size() == 0) {
                search = false;
            }
        }
        Integer m = GlobalConsts.NO;
        List<ChannelMergeSettleVO> channelMergeSettleVOList = new ArrayList<>(query.getPageSize());
        if (channelId != null && search) {
            ChannelMergeSettle mergeSettle = channelMergeSettleService.findById(channelId);
            if (mergeSettle != null && mergeSettle.getMergeSettle().equals(GlobalConsts.YES)) {
                m = mergeSettle.getMergeSettle();
                query.setChannelId(null);
                query.setMergeSettle(GlobalConsts.YES);
                query.setMergeMark(mergeSettle.getMergeMark());
                if (channelIdSearchList.size() > 0) {
                    query.setChannelIdList(channelIdSearchList);
                }
                List<ChannelMergeSettle> items = channelMergeSettleService.listPageByQuery(query);
                List<Integer> channelIdList = items.stream().map(ChannelMergeSettle::getChannelId).collect(Collectors.toList());
                channelIdList.add(channelId);
                List<ChannelIdAndName> channelIdAndNameList = channelService.listNameByChannelIds(channelIdList);
                Map<Integer, String> channelIdAndNameMap = channelIdAndNameList.stream().collect(Collectors.toMap(ChannelIdAndName::getChannelId, ChannelIdAndName::getName));
                items.forEach( i -> {
                    ChannelMergeSettleVO vo = ChannelMergeSettleVO.builder()
                            .channelId(i.getChannelId())
                            .mergeMark(i.getMergeMark())
                            .name(channelIdAndNameMap.get(i.getChannelId()))
                            .build();
                    channelMergeSettleVOList.add(vo);
                });
            }
        }
        ModelAndView mav = new ModelAndView("channel/mergeSettle/list");
        mav.addObject("items", channelMergeSettleVOList);
        query.setChannelId(channelId);
        mav.addObject("vo", query);
        Channel obj = channelService.findByKey(channelId);
        obj.setMergeSettle(m);
        mav.addObject("obj", obj);
        mav.addObject("flag", 15);
        return mav;
    }

}
